package com.molyfun.parents.service.filedownload;

import android.content.Context;
import android.content.Intent;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.dao.entity.SingleDownloadTask;
import com.molyfun.parents.dao.gen.DaoMaster;
import com.molyfun.parents.dao.gen.DaoSession;
import com.molyfun.parents.dao.gen.SingleDownloadTaskDao;
import com.molyfun.parents.rest.bean.CourseListResponse;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.FileUtils;

import java.util.ArrayList;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/1/16.
 */
public class CourseDownLoadUtils {

    /**
     * download course single
     * @param context
     * @param courseBean
     */
    public static void startDownLoadCourse(Context context, CourseListResponse.CourseBean courseBean) {

        ArrayList<String> courseIdList = new ArrayList<>();
        if(courseBean != null){
            courseIdList.add(courseBean.getId());
        }

        Intent intent = new Intent();
        intent.setClass(context.getApplicationContext(), CourseDownloadService.class);
        intent.putStringArrayListExtra(CourseDownloadService.FLAG_COURSE_ID_LIST, courseIdList);
        context.startService(intent);
    }

    /**
     * download course mult
     * @param context
     * @param courseBeanList
     */
    public static void startDownLoadCourse(Context context, ArrayList<CourseListResponse.CourseBean> courseBeanList) {
        ArrayList<String> courseIdList = new ArrayList<>();
        for(CourseListResponse.CourseBean courseBean : courseBeanList){
            courseIdList.add(courseBean.getId());
        }
        Intent intent = new Intent();
        intent.setClass(context.getApplicationContext(), CourseDownloadService.class);
        intent.putStringArrayListExtra(CourseDownloadService.FLAG_COURSE_ID_LIST, courseIdList);
        context.startService(intent);
    }

    /**
     * update single media
     * @param context
     * @param singleDownloadTask
     */
    public static void updateGameAttachment(Context context, SingleDownloadTask singleDownloadTask, String url) {
        Intent intent = new Intent();
        intent.setClass(context.getApplicationContext(), CourseDownloadService.class);
        intent.putExtra(CourseDownloadService.FLAG_COURSE_DOWNLOAD_MODE, CourseDownloadService.CourseDownloadServiceMode.FLAG_MATERIAL_UPDATE);
        intent.putExtra(CourseDownloadService.FLAG_SINGLE_GAME_ID, singleDownloadTask.getId());
        intent.putExtra(CourseDownloadService.FLAG_SINGLE_GAME_URL, url);
        context.startService(intent);
    }


    /**
     * re-download file for that failed
     * @param context
     * @param singleDownloadTasks
     */
    public static void startDownLoadFiledMedia(Context context, ArrayList<SingleDownloadTask> singleDownloadTasks) {
        long[] downloadTaskIds = new long[singleDownloadTasks.size()];
        for(int position = 0; position < singleDownloadTasks.size() ; position ++){
            downloadTaskIds[position] = singleDownloadTasks.get(position).getId();
        }

        Intent intent = new Intent();
        intent.setClass(context.getApplicationContext(), CourseDownloadService.class);
        intent.putExtra(CourseDownloadService.FLAG_COURSE_DOWNLOAD_MODE, CourseDownloadService.CourseDownloadServiceMode.FLAG_MATERIAL_DOWNLOAD);
        intent.putExtra(CourseDownloadService.FLAG_MEDIA_LIST, downloadTaskIds);
        context.startService(intent);
    }

    /**
     * remove download files
     * @param courseDownloadId
     */
    public static void delDownLoadCourseMediaList(String courseDownloadId){
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MFPApplication.getInstance(), Const.DB_NAME, null);
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        SingleDownloadTaskDao singleDownloadTaskDao = daoSession.getSingleDownloadTaskDao();
        for(SingleDownloadTask singleDownloadTask : singleDownloadTaskDao.queryBuilder().where(SingleDownloadTaskDao.Properties.DownloadKeyId.eq(courseDownloadId)).build().list()){
            singleDownloadTaskDao.delete(singleDownloadTask);
            FileUtils.deleteFile(singleDownloadTask.getPath());
        }
    }
}
